package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.chapter.ChapterVo;
import com.atguigu.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.serviceedu.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-08-29
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    @GetMapping("allChapters/{courseId}")
    public R getAllChapters(@PathVariable("courseId") String courseId){
        List<ChapterVo> list=chapterService.getAllChaptersByCourseId(courseId);
        return R.ok().data("list",list);
    }
    @ApiOperation(value = "新增章节")
    @PostMapping("addChapter/{courseId}")
    public R save(@PathVariable("courseId") String courseId,
            @ApiParam(name = "chapterVo", value = "章节对象", required = true)
            @RequestBody EduChapter chapter){
        chapter.setCourseId(courseId);
        chapterService.save(chapter);
        return R.ok();
    }
    @PostMapping("updateChapter")
    public R update(@RequestBody EduChapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }
    @GetMapping("getChapter/{id}")
    public R getById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id){
        EduChapter chapter = chapterService.getById(id);
        return R.ok().data("item", chapter);
    }
    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("delete/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id){
        boolean result = chapterService.deleteChapterById(id);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }
}

